
import queryString from 'query-string';
import { routerRedux } from 'dva/router';
import { message } from 'antd';
import { isSuccess } from '../../utils/handleError';
import { findQuestion, addQuestion, updateQuestion, uploadPic } from '../../services/faqManage';
import { cutOutImgUrl } from '../../utils';

export default {
    namespace: 'faqDetail',
    state: {
        data: {
            id: '',
            errorCode: '',
            question: '',
            answer: []
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen((location) => {
                if (location.pathname.includes('faqDetail')) {
                    const { id } = queryString.parse(location.search);
                    console.log({id})
                    if (id) {
                        dispatch({
                            type: 'query',
                            payload: { id },
                        })
                    } else {
                        dispatch({
                            type: 'updateData',
                            payload: {
                                id: '',
                                errorCode: '',
                                question: '',
                                answer: []
                            },
                        })
                    }
                }
            })
        },
    },
    effects: {
        * query({
            payload
        }, { put, call }) {
            const res = yield call(findQuestion, payload);
            if (res.code === '200') {
                yield put({
                    type: 'updateState',
                    payload: {
                        data: res.data
                    }
                });
            }else{
                message.error(res.msg)
            }
        },
        * save({
            payload
        }, { select, put, call }) {
            const { data } = yield select(_ => _.faqDetail);
            data.answer.forEach(element => element.pictureUrl = cutOutImgUrl(element.pictureUrl));
            const hide = message.loading('保存中...')
            const res = yield call(data.rowguid ? updateQuestion : addQuestion, data);
            setTimeout(hide, 0);
            if (res.code === '200') {
                message.success('保存成功！')
                yield put(routerRedux.push({
                    pathname: '/chzu/faqManage',
                }))
            }else{
                message.error(res.msg)
            }
        },
        * upload({
            payload
        }, { select, put, call }) {
            const { fileList, file ,index} = payload;
            if(file.status=="removed"){
                yield put({
                    type: 'updatePic',
                    payload: {
                        pictureUrl: '',
                        index,
                    }
                });
            }else{
                const { data: { errorCode } } = yield select(_ => _.faqDetail);
                if (!errorCode) {
                    message.warn('请先输入错误代码！')
                    return
                }
                const formData = new FormData();
                const imgFile = file;
                formData.append('file', imgFile);
                const hide = message.loading('上传中...')
                const res = yield call(uploadPic, formData, errorCode);
                setTimeout(hide, 0);
                if (res.code === '200') {
                    message.success('上传成功！')
                    const { data } = res;
                    yield put({
                        type: 'updatePic',
                        payload: {
                            pictureUrl: data.url,
                            index,
                        }
                    });
                }else{
                    message.error(res.msg)
                }
            }
        },
    },
    reducers: {
        updateState(state, { payload }) {
            return {
                ...state,
                ...payload,
            }
        },
        updateData(state, { payload }) {
            return {
                ...state,
                data: {
                    ...state.data,
                    ...payload,
                }
            }
        },
        addAnswer(state, { payload }) {
            const { data } = state;
            data.answer = [
                ...data.answer,
                {
                    order: new Date().getTime(),
                    pictureUrl: '',
                    text: '',
                }
            ]
            return {
                ...state,
                data,
            }
        },
        removeAnswer(state, { payload }) {
            const { data } = state;
            data.answer.splice(payload, 1)
            return {
                ...state,
                data,
            }
        },
        updateText(state, { payload }) {
            const { data } = state;
            const { value, index } = payload;
            data.answer[index].text = value
            return {
                ...state,
                data,
            }
        },
        updatePic(state, { payload }) {
            const { data } = state;
            const { pictureUrl, index } = payload;
            data.answer[index].pictureUrl = pictureUrl;
            return {
                ...state,
                data,
            }
        }
    },
}